<?php

class Admin extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{admin}}':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var integer $role_id
	 * @var boolean $is_active
	 * @var string $create_time
	 * @var string $update_time
	 */
	
	public $verifyPassword;
	private $_oldPassword;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, role_id', 'required'),
			//array('password, verifyPassword', 'required'),
			array('role_id', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>128),
			array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => Yii::t("main", "The passwords you entered don't match")),
			array('is_active', 'boolean'),
			array('create_time, update_time', 'unsafe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, role_id, is_active, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','Id'),
			'username' => Yii::t('main','Username'),
			'password' => Yii::t('main','Password'),
			'verifyPassword' => Yii::t('main','Verify Password'),
			'role_id' => Yii::t('main','Role'),
			'is_active' => Yii::t('main','Is Active'),
			'create_time' => Yii::t('main','Create Time'),
			'update_time' => Yii::t('main','Update Time'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);

		$criteria->compare('t.username',$this->username,true);

		$criteria->compare('t.password',$this->password,true);

		$criteria->compare('t.role_id',$this->role_id);

		$criteria->compare('t.is_active',$this->is_active);

		$criteria->compare('t.create_time',$this->create_time,true);

		$criteria->compare('t.update_time',$this->update_time,true);

		return new CActiveDataProvider('Admin', array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>Setting::get('PerPage')),
		));
	}
	
	public function validatePassword($password)
	{
		return md5($password)===$this->password;
	}
	
	public function beforeSave()
	{
		if ($this->password){
			$this->password=md5($this->password);
		} else {
			$this->password=$this->_oldPassword;
		}
		return parent::beforeSave();
	}
	
	public function afterFind()
	{
		$this->_oldPassword=$this->password;
		return parent::afterFind();
	}
	
	public function behaviors()
    {
        return array(
			'History' => array(
                'class'=>'application.components.behaviors.HistoryBehavior',
            ),
			'OldAttributes' => array(
				'class'=>'application.components.behaviors.OldAttributesBehavior',
			),
            'NotActiveOnDelete' => array(
            	'class'=>'application.components.behaviors.NotActiveOnDeleteBehavior',
            ),
        );
    }
}